<?php

class Jd_buniess_balance_model extends MY_Model {

    public function __construct()
    {
        $this->modelename='Jd_buniess_balance_model';
        $this->tablename='buniess_balance';
        parent::__construct();

    }


   public function addlog($row,$date){
        $time=time();
        foreach ($row as $id=>$v){
            if($v['balance']!=0){
                $v['date']=$date;
                $v['addtime']=$time;
                $result= $this->CheckAndDel($v);
                if($result){
                    $balance= $v['balance'];
                    $user_id=$v['user_id'];
                    $date=$v['date'];
                    $sql="update   jd_buniess_balance   set `update`=$time, balance=$balance    where  user_id=$user_id AND `date`='$date'";
                    $this->db->query($sql);
                }else{
                    $this->add($this->tablename,$v);
                }

            }
        }

   }


    /**
     *
     * 检查是否已经存在数据
     * @param $row
     * @return bool
     */
   public function CheckAndDel($row){
        $user_id=$row['user_id'];
        $date=$row['date'];
        $sql="select 1 from jd_buniess_balance where user_id=$user_id AND `date`='$date'";
        $result=$this->GetBySql($sql);
        if(!empty($result)){
            return true;
        }
        return false;
   }
















}
